package com.sttk.secreconeclick.bookkeep.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookBean;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AccountBookAdapter extends RecyclerView.Adapter<AccountBookAdapter.ViewHolder> {
    private List<AccountBookBean> mDatas;
    private int selPosition;

    public AccountBookAdapter() {
        mDatas = new ArrayList<>();
    }

    public void setList(List<AccountBookBean> list) {
        this.mDatas = list;
        notifyDataSetChanged();
    }

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_account_book, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        AccountBookBean bean = mDatas.get(position);

        // 设置账本名称
        holder.tvName.setText(bean.getName());

        // 设置账本类型
        holder.tvType.setText(bean.getTypeName());

        // 设置记录数量
        int recordSize = bean.getBillBeanList() != null ? bean.getBillBeanList().size() : 0;
        holder.tvSize.setText(holder.itemView.getContext().getString(R.string.account_book_size_num, recordSize));

        // 计算收支结余
        double totalIncome = 0;
        double totalExpenditure = 0;

        if (bean.getBillBeanList() != null) {
            for (int i = 0; i < bean.getBillBeanList().size(); i++) {
                double money = bean.getBillBeanList().get(i).getMoney();
                if (bean.getBillBeanList().get(i).isIncome()) {
                    totalIncome += money;
                } else {
                    totalExpenditure += money;
                }
            }
        }

        double balance = totalIncome - totalExpenditure;

        // 设置收支结余文本，保留两位小数
        holder.tvIncome.setText(holder.itemView.getContext().getString(R.string.all_income_text) + String.format("%.2f", totalIncome));
        holder.tvExpenditure.setText(holder.itemView.getContext().getString(R.string.all_expenditure_text) + String.format("%.2f", totalExpenditure));
        holder.tvBalance.setText(holder.itemView.getContext().getString(R.string.balance_text) + String.format("%.2f", balance));

        // 选中状态
        holder.ivIcon.setImageResource(position == selPosition ? R.drawable.xuanzhong : R.drawable.zhangben);

        // 点击事件
        holder.itemView.setOnClickListener(view -> {
            if (listener != null) {
                listener.clickItem(position);
            }
        });

        // 编辑按钮点击事件
        holder.ivEdit.setOnClickListener(view -> {
            if (listener != null) {
                listener.clickEdit(position);
            }
        });

        // 删除按钮点击事件
        holder.ivDelete.setOnClickListener(view -> {
            if (listener != null) {
                listener.clickDelete(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    // 拖动排序逻辑
    public void onItemMove(int fromPos, int toPos) {
        Collections.swap(mDatas, fromPos, toPos);
        List<AccountBookBean> listSave = new ArrayList<>();
        listSave.addAll(mDatas);
        AppSpUtils.saveAccountBookList(listSave);
        notifyItemMoved(fromPos, toPos);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivIcon, ivEdit, ivDelete;
        TextView tvName, tvType, tvSize, tvIncome, tvExpenditure, tvBalance;

        ViewHolder(View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.ivAccountBookItemImg);
            ivEdit = itemView.findViewById(R.id.ivAccountBookItemEdit);
            ivDelete = itemView.findViewById(R.id.ivAccountBookItemDelete);

            tvName = itemView.findViewById(R.id.tvAccountBookItemName);
            tvType = itemView.findViewById(R.id.tvAccountBookItemType);
            tvSize = itemView.findViewById(R.id.tvAccountBookItemSize);
            tvIncome = itemView.findViewById(R.id.tvAccountBookItemIncome);
            tvExpenditure = itemView.findViewById(R.id.tvAccountBookItemExpenditure);
            tvBalance = itemView.findViewById(R.id.tvAccountBookItemBalance);
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickItem(int pos);

        void clickEdit(int pos);

        void clickDelete(int pos);
    }
}
